#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e6+1000;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL s1[maxn],s2[maxn],d[maxn],m,n;
inline void pre(){
    int i=1;int j=0;
    while(i<n){
        if(s2[i]==s2[j]){
            d[i++]=++j;
        }
        else{
            if(j) j=d[j-1];
            else i++;
        }
    }
}
inline LL solve(){
    memset(d,0,n*
           sizeof(LL));
    pre();
    int i=0;int j=0;
    while(i<m){
        if(s1[i]==s2[j]){
            i++;j++;
            if(j==n){
                return (i+1)-n;
            }
        }
        else{
            if(j) j=d[j-1];
            else i++;
        }
    }
    return -1;
}
int main(void){
   	cin.tie(0);std::ios::sync_with_stdio(false);
    int t;cin>>t;
    while(t--){
        cin>>m>>n;
        for(int i=0;i<m;i++) cin>>s1[i];
        for(int i=0;i<n;i++) cin>>s2[i];
        cout<<solve()<<endl;
    }
   	return 0;
}
